//
//  SearchContentCollectionViewCellThird.m
//  XYFMApp
//
//  Created by 岁变 on 16/9/10.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "SearchContentCollectionViewCellThird.h"
#import "XYMacro.h"

@interface SearchContentCollectionViewCellThird () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) BaseTableView *tableView;

@property (nonatomic, strong) NSMutableArray *mArr;
@property (nonatomic, assign) NSInteger count;

@end

@implementation SearchContentCollectionViewCellThird

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        _count = 1;
        _mArr = [NSMutableArray array];
        _tableView = [[BaseTableView alloc] initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = BACKGROUNDCOLOR;
        [_tableView registerClass:[SearchContentTableViewCellFirst class] forCellReuseIdentifier:@"SearchContentTableViewCellFirst"];
        [self.contentView addSubview:_tableView];
        [self pullDownRefresh];
        
    }
    return self;
}


- (void)pullDownRefresh {
    MJRefreshGifHeader *header = [MJRefreshGifHeader  headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [self.tableView.mj_header beginRefreshing];
    
    NSMutableArray *mArr = [NSMutableArray array];
    for (int i = 0; i < 3; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"list_load_%d", i]];
        [mArr addObject:image];
    }
    
    [header setImages:mArr forState:MJRefreshStateRefreshing];
    header.stateLabel.hidden = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.mj_header = header;
    
}


- (void)loadNewData {
    
    NSString *str1 = @"json=%7B%22cat_id%22%3A%220%22%2C%22searchtype%22%3A2%2C%22target_name%22%3A%22";
    NSString *str2 = @"%22%2C%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22keyword%22%3A%22%22%2C%22orderby%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%2C%22target_id%22%3A%220%22%7D";
    NSString *body = [[str1 stringByAppendingString:_keyword] stringByAppendingString:str2];
    
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:body andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
        SearchModelFirst *model = [SearchModelFirst modelWithDictionary:result];
        if (model.status.succeed == 1) {
            [_mArr removeAllObjects];
            _mArr = model.data;
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        }
        
    } andFailureBlock:^(NSError *error) {}];

    
    
}



- (void)setupUpRefresh {
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreBills)];
}

- (void)loadMoreBills {
    
    _count += 1;
    
    NSString *str1 = @"json=%7B%22cat_id%22%3A%220%22%2C%22searchtype%22%3A2%2C%22target_name%22%3A%22";
    NSString *str2 = @"%22%2C%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22keyword%22%3A%22%22%2C%22orderby%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A";
    NSString *str3 = @"%7D%2C%22target_id%22%3A%220%22%7D";
    NSString *body = [[[[str1 stringByAppendingString:_keyword] stringByAppendingString:str2] stringByAppendingString:[NSString stringWithFormat:@"%ld", _count]] stringByAppendingString:str3];
    
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:body andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
        SearchModelSecond *model = [SearchModelSecond modelWithDictionary:result];
        if (model.paginated.more == 1) {
            [_mArr addObjectsFromArray:model.data];
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
        } else {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
    } andFailureBlock:^(NSError *error) {}];

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _mArr.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SearchContentTableViewCellFirst *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchContentTableViewCellFirst" forIndexPath:indexPath];
    SearchModelFirstData *model = [_mArr objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    VCOfSearchConent *currentVC = (VCOfSearchConent *)self.nextResponder.nextResponder.nextResponder;
    VCOfDiscoverList *nextVC = [[VCOfDiscoverList alloc] init];
    SearchModelFirstData *model = [_mArr objectAtIndex:indexPath.row];
    nextVC.item_id = [NSString stringWithFormat:@"%ld", model.item_id];
    [currentVC.navigationController pushViewController:nextVC animated:YES];
    
}

- (void)setModel:(SearchModelFirst *)model {
    
    _model = [[SearchModelFirst alloc] init];
    _model = model;
    _mArr = model.data;
    [_tableView reloadData];
    
    if (model.paginated.more == 1) {
        [self setupUpRefresh];
    }

    
}





@end
